package com.bookstore.service;

import com.bookstore.dao.BookDAO;
import com.bookstore.model.Book;

import java.util.List;
import java.util.ArrayList;

public class BookService {
    private BookDAO bookDAO;

    public BookService() {
        this.bookDAO = new BookDAO();
    }

    public boolean addBook(Book book) {
        // 在这里可以添加业务逻辑，例如验证ISBN格式、价格范围等
        if (book.getIsbn() == null || book.getIsbn().trim().isEmpty()) {
            System.err.println("Error: ISBN cannot be empty.");
            return false;
        }
        if (book.getTitle() == null || book.getTitle().trim().isEmpty()) {
            System.err.println("Error: Title cannot be empty.");
            return false;
        }
        if (book.getPrice() < 0) {
            System.err.println("Error: Price cannot be negative.");
            return false;
        }
        if (book.getStock() < 0) {
            System.err.println("Error: Stock cannot be negative.");
            return false;
        }
        return bookDAO.addBook(book);
    }

    public List<Book> getAllBooks() {
        return bookDAO.getAllBooks();
    }

    public boolean updateBook(Book book) {
        if (book.getIsbn() == null || book.getIsbn().trim().isEmpty()) {
            System.err.println("Error: ISBN cannot be empty for update.");
            return false;
        }
        if (book.getTitle() == null || book.getTitle().trim().isEmpty()) {
            System.err.println("Error: Title cannot be empty for update.");
            return false;
        }
        if (book.getPrice() < 0) {
            System.err.println("Error: Price cannot be negative for update.");
            return false;
        }
        if (book.getStock() < 0) {
            System.err.println("Error: Stock cannot be negative for update.");
            return false;
        }
        return bookDAO.updateBook(book);
    }

    public boolean deleteBook(String isbn) {
        if (isbn == null || isbn.trim().isEmpty()) {
            System.err.println("Error: ISBN cannot be empty for deletion.");
            return false;
        }
        return bookDAO.deleteBook(isbn);
    }

    public Book getBookByIsbn(String isbn) {
        return bookDAO.getBookByIsbn(isbn);
    }

    public boolean increaseStock(String isbn, int quantity) {
        if (isbn == null || isbn.trim().isEmpty()) {
            System.err.println("Error: ISBN cannot be empty for increasing stock.");
            return false;
        }
        if (quantity <= 0) {
            System.err.println("Error: Quantity must be positive for increasing stock.");
            return false;
        }
        return bookDAO.increaseStock(isbn, quantity);
    }

    public boolean sellBook(String isbn, int quantity) {
        if (isbn == null || isbn.trim().isEmpty()) {
            System.err.println("Error: ISBN cannot be empty for selling book.");
            return false;
        }
        if (quantity <= 0) {
            System.err.println("Error: Quantity must be positive for selling book.");
            return false;
        }
        return bookDAO.sellBook(isbn, quantity);
    }

    public List<Book> searchBooks(String query) {
        if (query == null || query.trim().isEmpty()) {
            // If query is empty, return all books or handle as per business rule
            return getAllBooks();
        }
        return bookDAO.searchBooks(query);
    }

    public List<Book> getLowStockBooks(int threshold) {
        if (threshold < 0) {
            System.err.println("Error: Threshold cannot be negative.");
            return new ArrayList<>();
        }
        return bookDAO.getLowStockBooks(threshold);
    }
} 